package com.zys.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson2.JSON;
import com.zys.test.WeChatUser;
import com.zys.test.WechatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class Test1Controller {

    @GetMapping("/wxCheck")
    public String wxCheck(@RequestParam(value = "signature") String signature,
                          @RequestParam(value = "timestamp") String timestamp,
                          @RequestParam(value = "nonce") String nonce,
                          @RequestParam(value = "echostr") String echostr) {

        log.info("收到微信校验请求，echoststr:{}", echostr);
        return echostr;
    }

    @GetMapping("/twxLogin")
    public void wxLoginPage(HttpServletResponse response) throws Exception {
        String redirectUrl = URLEncoder.encode(
                "http://4c4e7f73.r27.cpolar.top/test/wxCallback?from=popup",
                StandardCharsets.UTF_8
        );

        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx73a67e59e6c01244&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        response.setContentType("image/png");

        QrCodeUtil.generate(url, 300, 300, "jpg", response.getOutputStream());
    }

}
